package com.dkm.ec.so.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.dkm.base.api.model.Page;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.so.domain.SaleReview;
import com.dkm.ec.so.persistence.dao.SaleReviewQueryDao;
import com.dkm.ec.so.persistence.entity.SaleReviewPo;
import com.dkm.ec.so.repository.SaleReviewRepository;

/**
 * 对象功能:产品评价 Repository接口的实现类
 * 开发公司:SKG.COM
 * 开发人员:颜超敏
 * 创建时间:2015-02-10 11:03:42
 */
@Repository
public class SaleReviewRepositoryImpl extends AbstractRepository<String, SaleReviewPo,SaleReview> implements SaleReviewRepository{
	  
	@Resource
	private  SaleReviewQueryDao saleReviewQueryDao;

	public SaleReview newInstance() {
		SaleReviewPo po = new SaleReviewPo();
		SaleReview saleReview = SpringHelper.getBean(SaleReview.class);
		saleReview.setData(po);
		return saleReview;
	}

	public SaleReview newInstance(SaleReviewPo po) {
		SaleReview saleReview = SpringHelper.getBean(SaleReview.class);
		saleReview.setData(po);
		return saleReview;
	} 
	
	@Override
	protected IQueryDao<String, SaleReviewPo> getQueryDao() {
		return saleReviewQueryDao;
	}

	@Override
	public List<SaleReviewPo> findSaleReviewForFrontPaged(String userId,
			String soId, String prodId, String skuId, Page page,String sort,String id, String type) {
		Map<String, Object>params = new HashMap<String, Object>();
		if(StringUtils.isEmpty(userId))userId = null;
		if(StringUtils.isEmpty(soId))soId = null;
		if(StringUtils.isEmpty(prodId))prodId = null;
		if(StringUtils.isEmpty(skuId))skuId = null;
		if (StringUtils.isNotEmpty(sort)) {
			params.put("sort", sort);
		}
		if (StringUtils.isNotEmpty(id)) {
			params.put("id", id);
		}
		if (StringUtils.isNotEmpty(type) && "show".equalsIgnoreCase(type)) {
			params.put("type", type);
		}
		params.put("createdBy", userId);
		params.put("soId", soId);
		params.put("prodId", prodId);
		params.put("skuId", skuId);
		return saleReviewQueryDao.findByKey("findSaleReviewForFrontPaged", params,page);
		
	}

	@Override
	public Integer countSaleReviewForFront(String userId, String soId,
			String prodId, String skuId, String type) {
		Map<String, Object>params = new HashMap<String, Object>();
		if(StringUtils.isEmpty(userId))userId = null;
		if(StringUtils.isEmpty(soId))soId = null;
		if(StringUtils.isEmpty(prodId))prodId = null;
		if(StringUtils.isEmpty(skuId))skuId = null;
		if (StringUtils.isNotEmpty(type) && "show".equalsIgnoreCase(type)) {
			params.put("type", type);
		}
		params.put("createdBy", userId);
		params.put("soId", soId);
		params.put("prodId", prodId);
		params.put("skuId", skuId);
		return saleReviewQueryDao.countByKey("countSaleReviewForFront", params);
	}

	@Override
	public List<SaleReviewPo> findStarAndCountByProduId(String prodId) {
		Map<String, Object>params = new HashMap<String, Object>();
		params.put("prodId", prodId);
		return saleReviewQueryDao.findByKey("findStarAndCountByProduId", params);
	}

	@Override
	public SaleReviewPo getAvgScoreByProduId(String prodId) {
		Map<String, Object>params = new HashMap<String, Object>();
		params.put("prodId", prodId);
		return saleReviewQueryDao.getByKey("getAvgScoreByProduId", params);
	}
	 public Integer countReview(Map<String, Object> params){
		 return saleReviewQueryDao.count(params);
	 }

	 /**
	  * 查找产品回复
	  * @param reviewId
	  * @return 
	  * boolean
	  * @exception 
      * @since  1.0.0
	  */
	public boolean findSaleReviewById(String reviewId) {
		SaleReviewPo saleReviewPo = saleReviewQueryDao.get(reviewId);
		if(saleReviewPo != null){
			return true;
		}
		return false;
	}
	/**
	 * 查找用户评论及购买商品信息
	 */
	@Override
	public List<SaleReviewPo> findUserReviewAndSoItem(String userId,Page page) {
		return saleReviewQueryDao.findUserReviewAndSoItem(userId,page);
	}
	/**
	 * 根据产品skuId获取评论力图片数 
	 */
	@Override
	public List<SaleReviewPo> findImageBySkuId(String prodSkuId,String prodId) {
		Map<String, Object> params = new HashMap<>();
		params.put("skuId", prodSkuId);
		params.put("prodId", prodId);
		return saleReviewQueryDao.findByKey("findSaleReviewTopTen", params);
	}

	@Override
	public List<SaleReviewPo> findByPartyNameAndCreateTime(String partyName, String createTime) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SaleReviewPo> findBySaleEntityId(String saleEntityId, Page page,String reviewType) {
		String sql = "t1.prod_id_='"+saleEntityId+"'";
		Map<String, Object> params = new HashMap<String, Object>();
        if(reviewType != null){
        	//good_review=好评；middle_review=中评；mistake_review=差评。
        	sql += " and t1.type_='"+reviewType+"'";
        }
        params.put("whereSql", sql);
		return findPaged(params, page);
	}
	
}
